/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package in.co.sneh.controller.FileUploadServlet;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFormattedTextField;

/**
 *
 * @author Balrog
 */
public class ReglasVerificacion {
    
    String Palabra;
    String Inicio;
    Diccionarios diccionario=new Diccionarios();
    
    public boolean ReglasVerificacion(String palabras){
      if(Fecha_Vuelo(palabras)) return false;
        if(compania(palabras))return false;
        if(Vuelo(palabras))return false;
        if(Aeropuerto(palabras))return false;
        if(Hora_entrada(palabras))return false;
        if(Hora_salida(palabras))return false;
        if(Tiempo_tef(palabras))return false;
        if(corral(palabras))return false;
        if(Negocio(palabras))return false;     
     return true;
    }
  
    
    
    public boolean Start_Shit(String palabras){
        
        if(!Character.isDigit(palabras.charAt(0))) return false;
        if(!Character.isLetter(palabras.charAt(0))) return false;
        if(Character.isSpaceChar(palabras.charAt(0))) return false;
        
        return true;
    } 
      public boolean End_Shit(String palabras){
        
        if(!Character.isDigit(palabras.charAt(palabras.length()-1))) return false;
        if(!Character.isLetter(palabras.charAt(palabras.length()-1))) return false;
        if(Character.isSpaceChar(palabras.charAt(palabras.length()-1))) return false;
        if(Character.isSpace(palabras.charAt(palabras.length()-1))) return false;
        if(Character.toString(palabras.charAt(palabras.length()-1))=="") return false;
        return true;
    }
    
  
    
       public boolean Fecha_Vuelo(String palabras){
     //if( Start_Shit(palabras)) return false;
     //if( End_Shit(palabras)) return false;
     String a[]=palabras.split("-");
     if (!(a.length==3)) return false;
    Pattern pat = Pattern.compile("^[\\d][\\d]$");
    Pattern pat2 = Pattern.compile("^[\\w]\\w[\\w]$"); 
   
     Matcher mat = pat.matcher(a[0]);
     Matcher mat2 = pat2.matcher(a[1]);
     Matcher mat3 = pat.matcher(a[2]);
      
     if (!(mat.find() && mat2.find() && mat3.find())) return false;
 //   a[0]=String.format("%02d", Integer.parseInt(a[0]));
   // a[2]=String.format("%02d", Integer.parseInt(a[0]));
        if (!diccionario.dias(a[0])) return false;
        if (!diccionario.meses(a[1])) return false;
        if (!diccionario.años2(a[2])) return false;         
         return true;
    }
     public boolean compania(String compania){
         if( Start_Shit(compania)) return false;
        if( End_Shit(compania)) return false;
        if (!diccionario.companias(compania)) return false;
    return true;
    }
     
      public boolean Vuelo(String vuelo){
        if(Start_Shit(vuelo)) {return false;}
        if(End_Shit(vuelo)) {return false;}
        Pattern pat2 = Pattern.compile("([a-z]|[A-Z]|\\s)+"); 
        Matcher mat = pat2.matcher(vuelo);
        Pattern pat = Pattern.compile("[^A-Za-z0-9]"); 
        Matcher mat2 = pat.matcher(vuelo);
     if ((mat.find())) return false;
     if ((mat2.find())) return false;
      if(vuelo.contains(" "))return false;

      //if(vuelo.contains("[\\w]"))return false;
        
        return true;
    }
     
     public boolean Vuelo1(String vuelo){
        if( Start_Shit(vuelo)) return false;
        if( End_Shit(vuelo)) return false;
        String a[]=vuelo.split(" ");
        if (!(a.length==2)) return false;
      Pattern pat = Pattern.compile("^[\\d]\\d\\d[\\d]$");
      Matcher mat2 = pat.matcher(a[1]);
    Pattern pat2 = Pattern.compile("^[\\w][\\w]$"); 
    Matcher mat = pat2.matcher(a[0]);
     
      if (!(mat.matches() && mat2.matches() )) return false;
        if (!diccionario.companias(a[0])) return false; 
        if(!diccionario.Numero_Vuelo(a[1])) return false;
    return true;
    }
     
    public boolean Aeropuerto(String aeropuerto){
        if( Start_Shit(aeropuerto)) return false;
        if( End_Shit(aeropuerto)) return false;
        if (!diccionario.Aeropuertos(aeropuerto)) return false;
    return true;
  
    }
    
     public boolean Hora_entrada(String hora){
             if( Start_Shit(hora)) return false;
        if( End_Shit(hora)) return false;
        
          String a[]=hora.split(":");
        if (!(a.length==2)) return false;  
         if(!solonumero(a[0])) return false;
         if(!solonumero(a[1])) return false;
     Pattern pat = Pattern.compile("^[\\d][\\d]$");
     //Pattern pat2 = Pattern.compile("[a-z]");
     a[0]=String.format("%02d", Integer.parseInt(a[0]));
     a[1]=String.format("%02d", Integer.parseInt(a[0]));
     Matcher mat = pat.matcher(a[0]);
     Matcher mat2 = pat.matcher(a[1]);
    // Matcher mat3 = pat.matcher(a[2]);
      if (!(mat.find() && mat2.find() )) return false;
        if (!diccionario.horas(a[0])) return false; 
        if(!diccionario.minutos(a[1])) return false;
    return true;
    }
      public boolean Hora_salida(String hora){
                if( Start_Shit(hora)) return false;
        if( End_Shit(hora)) return false;
             String a[]=hora.split(":");
             if (!(a.length==2)) return false;
         if(!solonumero(a[0])) return false;
         if(!solonumero(a[1])) return false;
     Pattern pat = Pattern.compile("^[\\d][\\d]$");
     a[0]=String.format("%02d", Integer.parseInt(a[0]));
     a[1]=String.format("%02d", Integer.parseInt(a[1]));
     //Pattern pat2 = Pattern.compile("[a-z]");
     Matcher mat = pat.matcher(a[0]);
     Matcher mat2 = pat.matcher(a[1]);
    // Matcher mat3 = pat.matcher(a[2]);
      if (!(mat.find() && mat2.find() )) return false;
        if (!diccionario.horas(a[0])) return false; 
        if(!diccionario.minutos(a[1])) return false;
    return true;
    }
      
       public boolean Tiempo_tef(String hora){
                if( Start_Shit(hora)) return false;
        if( End_Shit(hora)) return false;
            String a[]=hora.split(":");
            if (!(a.length==3)) return false;
           if(!solonumero(a[0])) return false;
         if(!solonumero(a[1])) return false;
          if(!solonumero(a[2])) return false;
         Pattern pat = Pattern.compile("^[\\d]|[\\d]$");
         a[0]=String.format("%02d", Integer.parseInt(a[0]));
     a[1]=String.format("%02d", Integer.parseInt(a[1]));
     a[2]=String.format("%02d", Integer.parseInt(a[2]));
     
    //Pattern pat2 = Pattern.compile("[a-z]");
     Matcher mat = pat.matcher(a[0]);
     Matcher mat2 = pat.matcher(a[1]);
     Matcher mat3 = pat.matcher(a[2]);
     if (!(mat.find() && mat2.find() && mat3.find())) return false;
        if (!diccionario.horas(a[0])) return false; 
        if(!diccionario.minutos(a[1])) return false;
        if(!diccionario.segundos(a[2])) return false;
    return true;
    }
    public boolean corral(String corral){
         StringBuilder corralito = new StringBuilder();
         corralito.append(corral);
         corralito.append("&");
         String corral2;
        int flag=0;
        if(Start_Shit(corral)) {return false;}
        if(End_Shit(corral)) {return false;}
        // Pattern pat = Pattern.compile("[0-9]");
        Pattern pat2 = Pattern.compile("^[\\w].*[\\w]$"); 
     Matcher mat = pat2.matcher(corral);
     if (!(mat.find())) return false;
     
        for( int i=0;i<(corral.length());i++){
            corral2=corral.substring(0, i+1);
            if (diccionario.corral(corral2)) {flag=1;
            if (Character.toString(corralito.charAt(i+1))=="&") {return false;}// deberia ser al revez y igual funciona... revisar 
            }
        }
        if (flag==1) {return true;}
        else {return false;}

    }
     public boolean Negocio(String negocio){
            if(Start_Shit(negocio)) {return false;}
        if(End_Shit(negocio)) {return false;}
        if (negocio.length()>5) return false;
        Pattern pat2 = Pattern.compile("([0-9]|\\s)+"); 
        Matcher mat = pat2.matcher(negocio);
        Pattern pat = Pattern.compile("[^A-Za-z0-9]"); 
        Matcher mat2 = pat.matcher(negocio);
     if ((mat.find())) return false;
     if ((mat2.find())) return false;
      if(negocio.contains(" "))return false;
        return true;
    }
     public boolean solonumero(String cadena ){
         Pattern pat2 = Pattern.compile("([a-z]|[A-Z]|\\s)+"); 
        Matcher mat = pat2.matcher(cadena);
        Pattern pat = Pattern.compile("[^A-Za-z0-9]"); 
        Matcher mat2 = pat.matcher(cadena);
     if ((mat.find())) return false;
     if ((mat2.find())) return false;
      if(cadena.contains(" "))return false;
     return true;
     
     }
    
    
}
